package binding

import (
	"bhttp/validator"
	"binterfaces/ihttp"
	"net/http"
)

// 请求方式定义
const (
	BindingJson              = "application/json"
	BindingHtml              = "text/html"
	BindingXml               = "application/xml"
	BindingXml2              = "text/xml"
	BindingPlain             = "text/plain"
	BindingPostForm          = "application/x-www-form-urlencoded"
	BindingMultipartPostForm = "multipart/form-data"
	BindingProtobuf          = "application/x-protobuf"
	BindingMsgpack           = "application/x-msgpack"
	BindingMsgpack2          = "application/msgpack"
	BindingYaml              = "application/x-yaml"
	BindingToml              = "application/toml"
)

var Validator ihttp.IValidator = &validator.Validator{}

var (
	JSON          = &JsonBind{}
	Form          = &FormBind{}
	FormMultipart = &FormMultipartBind{}
)

// Default Biding的工厂方法
func Default(method, contentType string) ihttp.IBodyBind {
	if method == http.MethodGet {
		// TODO 处理 GET请求的绑定器
		panic("implement me")
	}
	switch contentType {
	case BindingJson:
		return JSON
	case BindingXml,
		BindingXml2,
		BindingProtobuf,
		BindingMsgpack,
		BindingMsgpack2,
		BindingYaml,
		BindingToml:
		return nil
	case BindingMultipartPostForm:
		return FormMultipart
	default: // case MIMEPOSTForm:
		return Form
	}
}

func validate(obj any) error {
	if Validator == nil {
		return nil
	}
	return Validator.Verify(obj)
}
